#include "qbrokenlinewidget.h"

#include "../../property/qabstractproperty.h"
#include "../../property/qpropertyfactory.h"
#include "../../shared/qcommonstruct.h"

#include <QPainter>
#include <QGradient>
#include <QPaintEngine>

QBrokenLineWidget::QBrokenLineWidget(QWidget *parent) :
    QWidget(parent)
{
}

void QBrokenLineWidget::setLineWidth(int lineWidth)
{
    if(lineWidth<1)
    {
        lineWidth=1;
    }
    if(m_line_width!=lineWidth)
    {
        m_line_width=lineWidth;
        update();
    }
}

int QBrokenLineWidget::lineWidth()
{
    return m_line_width;
}

void QBrokenLineWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    int line_style=property("line_style").toInt();
    QGradient g=property("line_color").value<QGradient>();
    QPen pen((Qt::PenStyle)line_style);
    pen.setBrush(QBrush(g));
    pen.setJoinStyle(Qt::RoundJoin);
    pen.setWidth(m_line_width);
    p.setPen(pen);

    p.setRenderHint(QPainter::Antialiasing,true);

    QPointFs ps=property("line_points").value<QPointFs>();

    QPoint* pt=new QPoint[ps.size()];

    int wid=this->width()-1-m_line_width;
    int hei=this->height()-1-m_line_width;

    int _x=m_line_width/2;
    int _y=m_line_width/2;

    for(int i=0;i<ps.size();i++)
    {
        QPointF pf=ps.at(i);
        pt[i].setX(_x+(int)(wid*pf.x()));
        pt[i].setY(_y+(int)(hei*pf.y()));
    }
    p.drawPolyline(pt,ps.size());

    delete [] pt;
}


QBrokenLineWidgetHost::QBrokenLineWidgetHost(QAbstractHost *parent):
    QWidgetHost(parent)
{
    this->setProperty("need_frame",true);
    setProperty("accept_drop",false);
    setProperty("edit_property","line_points");
}

void QBrokenLineWidgetHost::create_object()
{
    m_object=new QBrokenLineWidget;
    m_object->setObjectName("brokenLineWidget");
}

QString QBrokenLineWidgetHost::get_show_name()
{
    return tr("Broken Line");
}

QString QBrokenLineWidgetHost::get_show_group()
{
    return tr("Base Graphics");
}

QString QBrokenLineWidgetHost::get_show_icon()
{
    return ":/images/broken_line.png";
}

void QBrokenLineWidgetHost::init_property()
{
    QWidgetHost::init_property();

    remove_property("font");
    remove_property("enabled");
    remove_property("focusPolicy");
    remove_property("background");
    remove_property("border");
    remove_property("toolTip");
    remove_property("mouseMove");
    remove_property("mousePress");
    remove_property("mouseRelease");
    remove_property("doubleClick");

    QAbstractProperty* pro;

    pro=QPropertyFactory::create_property("Number");

    QBrokenLineWidget* wid=(QBrokenLineWidget*)m_object;
    if(pro!=NULL)
    {
        pro->set_property("name","line_width");
        pro->set_attribute("show_name",tr("Width"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_width",2);
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("PointFs");
    if(pro!=NULL)
    {
        pro->set_property("name","line_points");
        pro->set_attribute("show_name",tr("Points"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_points",pro->get_value());
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","line_color");
        pro->set_attribute("show_name",tr("Color"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_color",pro->get_value());
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Enum");
    if(pro!=NULL)
    {
        pro->set_property("name","line_style");
        pro->set_attribute("show_name",tr("Style"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        ComboItems items;
        tagComboItem item;
        item.m_text="SolidLine";
        item.m_value=Qt::SolidLine;
        items.append(item);
        item.m_text="DashLine";
        item.m_value=Qt::DashLine;
        items.append(item);
        item.m_text="DotLine";
        item.m_value=Qt::DotLine;
        items.append(item);
        item.m_text="DashDotLine";
        item.m_value=Qt::DashDotLine;
        items.append(item);
        item.m_text="DashDotDotLine";
        item.m_value=Qt::DashDotDotLine;
        items.append(item);
        QVariant v;
        v.setValue<ComboItems>(items);
        pro->set_attribute("items",v);
        m_object->setProperty("line_style",Qt::SolidLine);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,100,100));
}

void QBrokenLineWidgetHost::setLineWidth(int lineWidth)
{
    set_property_value("line_width",lineWidth);
}

int QBrokenLineWidgetHost::lineWidth()
{
    return get_property_value("line_width").toInt();
}

void QBrokenLineWidgetHost::setLineStyle(int lineStyle)
{
    set_property_value("line_style",lineStyle);
}

int QBrokenLineWidgetHost::lineStyle()
{
    return get_property_value("line_style").toInt();
}

qRegisterHost("basegraphics.broken_line",QBrokenLineWidgetHost,BASE_GRAPHICS_INDEX+50)
